package com.mxpio.erp.equipment.controller;

import com.mxpio.erp.common.enums.KnowledgeEnums;
import com.mxpio.erp.common.equipment.entity.EquipmentKnowledgeTag;
import com.mxpio.erp.common.equipment.entity.EquipmentTagRecord;
import com.mxpio.erp.common.equipment.service.EquipmentKnowledgeTagService;
import com.mxpio.erp.common.equipment.service.EquipmentTagRecordService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.JpaUtil;
import com.mxpioframework.jpa.policy.CrudContext;
import com.mxpioframework.jpa.policy.impl.SmartCrudPolicyAdapter;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;


@Tag(name = "EquipmentKnowledgeTagController", description = "知识库-标签")
@RestController
@RequestMapping("/erp/equipment/knowledge/tag/")
public class EquipmentKnowledgeTagController {

    @Autowired
    private EquipmentKnowledgeTagService equipmentKnowledgeTagService;
    @Autowired
    private EquipmentTagRecordService equipmentTagRecordService;



    @GetMapping("page")
    @Operation(summary = "知识库-标签列表", description = "获取知识库-标签列表（分页）", method = "GET")
    public Result<Page<EquipmentKnowledgeTag>> page(@Parameter(description="查询参数") Criteria criteria, @Parameter(description="每页条数")Integer pageSize, @Parameter(description="页号")Integer pageNo) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<EquipmentKnowledgeTag> pageResult = equipmentKnowledgeTagService.listPage(EquipmentKnowledgeTag.class, page, criteria);
        return Result.OK(pageResult);
    }

    @GetMapping("selectPage")
    @Operation(summary = "知识库-标签列表", description = "获取知识库-标签列表（分页）", method = "GET")
    public Result<Page<EquipmentKnowledgeTag>> selectPage(@Parameter(description="查询参数") Criteria criteria, @Parameter(description="每页条数")Integer pageSize, @Parameter(description="页号")Integer pageNo) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        criteria.addCriterion("tagStatus", Operator.EQ,KnowledgeEnums.TagStatus.USE.getCode());
        Page<EquipmentKnowledgeTag> pageResult = equipmentKnowledgeTagService.listPage(EquipmentKnowledgeTag.class, page, criteria);
        return Result.OK(pageResult);
    }

    @RequestMapping("add")
    @Operation(summary = "保存知识库-标签", description = "保存知识库-标签")
    public Result<EquipmentKnowledgeTag> add(@RequestBody EquipmentKnowledgeTag equipmentKnowledgeTag) {
        // 新增时默认使用中
        equipmentKnowledgeTag.setTagStatus(KnowledgeEnums.TagStatus.USE.getCode());
        equipmentKnowledgeTagService.save(equipmentKnowledgeTag);
        return Result.OK(equipmentKnowledgeTag);
    }

    @PutMapping("edit")
    @Operation(summary = "编辑知识库-标签", description = "编辑知识库-标签", method = "PUT")
    public Result<EquipmentKnowledgeTag> edit(@RequestBody EquipmentKnowledgeTag equipmentKnowledgeTag) {
        equipmentKnowledgeTagService.update(equipmentKnowledgeTag);
        return Result.OK(equipmentKnowledgeTag);
    }

    @PutMapping("changeStatus")
    @Operation(summary = "知识库-标签-状态变更", description = "编辑知识库-状态变更", method = "PUT")
    public Result<EquipmentTagRecord> changeStatus(@RequestBody EquipmentTagRecord equipmentTagRecord) {
        equipmentKnowledgeTagService.changeStatus(equipmentTagRecord);
        return Result.OK(equipmentTagRecord);
    }

    @GetMapping("record/page")
    @Operation(summary = "知识库-标签状态变更记录列表", description = "获取知识库-标签状态变更记录列表（分页）", method = "GET")
    public Result<Page<EquipmentTagRecord>> recordPage(@Parameter(description="查询参数") Criteria criteria, @Parameter(description="每页条数")Integer pageSize, @Parameter(description="页号")Integer pageNo) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<EquipmentTagRecord> pageResult = equipmentTagRecordService.listPage(EquipmentTagRecord.class, page, criteria);
        return Result.OK(pageResult);
    }

    @DeleteMapping("remove/{ids}")
    @Operation(summary = "根据id删除知识库-标签", description = "删除知识库-标签,多个id用英文逗号分隔", method = "DELETE")
    public Result<EquipmentKnowledgeTag> remove(@PathVariable(name = "ids", required = true) String ids) {
        String[] id = ids.split(",");
        for(String key : id){
            equipmentKnowledgeTagService.delete(EquipmentKnowledgeTag.class, key, new SmartCrudPolicyAdapter() {
                @Override
                public boolean beforeDelete(CrudContext context) {
                    Object o = context.getEntity();
                    if (o instanceof EquipmentKnowledgeTag){
                        try {
                            JpaUtil.lind(EquipmentTagRecord.class).equal("tagId", ((EquipmentKnowledgeTag) o).getId()).delete();
                        } catch (Exception e) {
                            return false;
                        }
                    }
                    return true;
                }
            });
        }
        return Result.OK();
    }


}
